<?php
/**
 * Created by PhpStorm.
 * User: 郑合达
 * Date: 2018/12/8
 * Time: 17:00
 */

namespace app\admin\controller;
use app\Model\TopicModel;
class Topic extends Common
{
    /**
     * @var int
     * 专题列表页面 搜索
     */
    private $page = 20;
    public function topic_List(){
        session('urls', null);
        if(input('searchdata')){
            if(input('searchdata')=="已发布"){
                $searchdata= '2';
            }elseif (input('searchdata')=="草稿"){
                $searchdata = '1';
            }else{
                $searchdata = input('searchdata');
            }
        }else{
            $searchdata="";
        }
        $res['list']= TopicModel::field('*')
            ->where('topic_title|topic_state','like','%'.$searchdata.'%')
            ->order('ctime')->paginate($this->page);
        $this->getConAndAct('content');
        return view('index',$res);
    }

    /**
     * @return \think\response\View
     * 增加专题页面
     */
    public function topic_add(){
        return view('add');
    }
    /**
     * @return \think\response\Json
     * 增加专题
     */
    public function add(){
        //数据验证
        $this->veri('Topic','add');

        $ret = TopicModel::create([
            'topic_title'        => input('topic_title'),
            'topic_image'         => input('topic_image'),
            'topic_content'       =>  input('topic_content'),
            'topic_state'      => input('topic_state'),
            'topic_margin_top'          => input('topic_margin_top'),
            'ctime'       =>time(),
        ]);
        if(!$ret){
            if (input('topic_state')==1){
                return retJson('', 201, '新增失败');
            }else{
                return retJson('', 201, '发布失败');
            }
        }
            if (input('topic_state')==1){
            return retJson('', 200, '新增成功');
        }else{
            return retJson('', 200, '新增成功');
        }
    }
    /**
     * @return \think\response\View
     * 跳转编辑页面，返回编辑参数
     */
    public function topic_edit(){
        $data= TopicModel::field('*')->where('topic_id',input('topic_id'))
            ->find();
        $this->assign('data',$data);
        return view('edit');
    }
    /**
     * @return \think\response\Json
     * 修改文章
     */
    public function edit(){
        //数据验证
        $this->veri('Topic','edit');

        //判断是否上传图片
        if (input('topic_image')!=1){
            //判断图片是否存在
            if (input('img')){
                unlink("./upload/topic/".input('img'));//删除图片
            }
            $img=input('topic_image');
        }else{
            $img=input('.img');
        }
        $data['topic_title']   = input('topic_title');
        $data['topic_image']   = $img;
        $data['topic_content']= input('topic_content');
        $data['topic_state'] = input('topic_state');
        $data['topic_margin_top']   = input('topic_margin_top');
        $data['ctime']   = time();
        // 文章信息修改
        $res = TopicModel::update($data, ['topic_id' => input('topic_id')]);
        if(!$res){
            return retJson('', 201, '修改失败');
        }

        return retJson('', 200, '修改成功');
    }
    /**
     * @return \think\response\Json
     * 删除专题
     */
    public function del(){
        if (input('topic_image')){
            if (file_exists("./upload/topic/".input('topic_image'))){
                unlink("./upload/topic/".input('topic_image'));//删除图片
            }
        }
        $del = TopicModel::destroy(input('id'));
        if(!$del){
            return retJson('', 201, '删除失败！');
        }
        return retJson('', 200, '删除成功！');
    }


    //图片上传
    public function webuploader(){
        if (session('urls')){
            unlink("./".session('urls'));//删除替换的图片
        }
        $file = $this->request->file('file');//file是传文件的名称
        $info = $file->move(ROOT_PATH . 'public/' . DS . '/upload/topic/');
        $names=str_replace("\\","/",$info->getSaveName());//把反斜杠(\)替换成斜杠(/)
        $temp_path='/upload/topic/';
        session('urls',$temp_path.$names);
        $arr = array(
            'link' =>$temp_path.$names,
            'filename' => $names,
        );
        return json($arr);
    }
}